package com.ywjh.inter.service;

import java.sql.Connection;
import java.util.List;

import com.ywjh.inter.conn.DBConnection;
import com.ywjh.inter.dao.InsulinPenInjectSetDAO;
import com.ywjh.inter.entity.InsulinPenInjectSetEntity;
import com.ywjh.inter.utils.HTTPHelper;

import net.sf.json.JSONObject;

public class InsulinPenInjectSetService {
	
	private static InsulinPenInjectSetService instance;
	private InsulinPenInjectSetDAO insulinPenInjectSetDAO;
	
	public static InsulinPenInjectSetService getInstance() {
		if(instance == null) {
			instance = new InsulinPenInjectSetService();
			instance.insulinPenInjectSetDAO = new InsulinPenInjectSetDAO();
		}
		return instance;
	}

	public boolean add(InsulinPenInjectSetEntity entity) {
		boolean b = false;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			b = insulinPenInjectSetDAO.insert(conn, entity);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return b;
	}
	
	public List<InsulinPenInjectSetEntity> getDocSet(int userId, int page, int num) {
		List<InsulinPenInjectSetEntity> r = null;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			r = insulinPenInjectSetDAO.getDocSet(conn, userId, page, num);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return r;
	}
	
	public boolean setDocSetOld(int userId) {
		boolean r = false;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			r = insulinPenInjectSetDAO.setDocSetOld(conn, userId);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return r;
	}
	
	public int getDocSetCount(int userId) {
		int r = 0;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			r = insulinPenInjectSetDAO.getDocSetCount(conn, userId);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return r;
	}
	
	public int getDocSetNewCount(int userId) {
		int r = 0;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			r = insulinPenInjectSetDAO.getDocSetNewCount(conn, userId);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return r;
	}
	
	public List<InsulinPenInjectSetEntity> getIssueLog(int userId, int page, int num) {
		List<InsulinPenInjectSetEntity> r = null;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			r = insulinPenInjectSetDAO.getIssueLog(conn, userId, page, num);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return r;
	}
	
	public int getIssueLogCount(int userId) {
		int r = 0;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			r = insulinPenInjectSetDAO.getIssueLogCount(conn, userId);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return r;
	}
	
	/**
	 * 下发注射信息到胰岛素笔
	 * @param entity
	 * @return
	 */
	public boolean issue(InsulinPenInjectSetEntity entity) {
		String url = "http://localhost:8095/amqp/service/pushOut";
		JSONObject message = new JSONObject();
		message.put("sn", entity.getSn());
		message.put("chn", "A");
		message.put("drug_id", entity.getDrugId());
		message.put("am_vlu", entity.getAmVlu());
		message.put("noon_vlu", entity.getNoonVlu());
		message.put("pm_vlu", entity.getPmVlu());
		message.put("base_vlu", entity.getBaseVlu());
		message.put("temp_vlu", entity.getTempVlu());
		message.put("drug_id2", "");
		message.put("method", "inject_msg");
		
		JSONObject data = new JSONObject();
		data.put("device", "insulin");
		data.put("message", message);
		
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			try {
				String res = HTTPHelper.postData(url, data, null);
				System.out.println(res);
				insulinPenInjectSetDAO.updateIssued(conn, entity.getId());
				return true;
			} catch(Exception e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return false;
	}
}
